package dome5Steam;

import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class Streamdome4 {
    public static void main(String[] args) {
        //stream终结方法相当于掌握stream的收集方法
        List<Teacher> teachers= new ArrayList<>();
        teachers.add(new Teacher("张无忌",46,99));
        teachers.add(new Teacher("张三丰",50,88));
        teachers.add(new Teacher("张翠山",48,60));
        teachers.add(new Teacher("张敏",18,100));
        teachers.stream().filter(t -> t.getAge()>40).forEach(System.out::println);//forEach为终结方法
        System.out.println("-----------------");
        long count = teachers.stream().filter(t -> t.getAge() > 40).count();
        System.out.println(count); System.out.println("-----------------");
        Optional<Teacher> max = teachers.stream().max((t1,t2)-> Double.compare(t1.getScore(),t2.getScore()));
        Teacher maxteacher = max.get();//获取Optional对象中的值,要用get方法
        System.out.println(maxteacher);
        System.out.println(count); System.out.println("-----------------");
        Optional<Teacher> min = teachers.stream().min((t1,t2)-> Double.compare(t1.getScore(),t2.getScore()));
        Teacher minteacher = min.get();//获取Optional对象中的值,要用get方法
        System.out.println(minteacher);
        System.out.println(count); System.out.println("--------stream收集方法---------");
        List<String> list=new ArrayList<>();
        list.add("张三分");
        list.add("张敏");
        list.add("八宝粥");
        list.add("张无忌");
        list.add("陈世玉");
        list.add("张家聪");
        list.add("张家聪");
        Stream<String> s1 = list.stream().filter(s -> s.startsWith("张"));
        //收集到list集合方法
        List<String> newlist=s1.collect(Collectors.toList());
        System.out.println(newlist);
        //收集到set集合方法
        System.out.println("----------------");
        Set<String> newset=new HashSet<>();//创建一个set集合 不用收集两次了
        newset.addAll(newlist);
        System.out.println(newset);
//        System.out.println("----------------");
//        Stream<String> s2 = list.stream().filter(s -> s.startsWith("张"));
//        Set<String> newset=s2.collect(Collectors.toSet());
//        System.out.println(newset);
        System.out.println("----------------");
        //收集到数组方法
        Stream<String> s3 = list.stream().filter(s -> s.startsWith("张"));
        Object[] arrays=s3.toArray();
        System.out.println(Arrays.toString(arrays));
        System.out.println("----------------");
        //收集到map方法
        //Stream<Teacher> s4 = teachers.stream().sorted((t1, t2) -> Double.compare(t1.getScore(), t2.getScore()));
        //收集到map集合，键是老师，值是老师对应的分数
//        Map<String, Double> map=teachers.stream().collect(Collectors.toMap(Teacher::getName, Teacher::getScore));
        Map<String, Double> map=teachers.stream().collect(Collectors.toMap(new Function<Teacher, String>() {
            @Override
            public String apply(Teacher teacher) {
                return teacher.getName();
            }
        }, new Function<Teacher, Double>() {
            @Override
            public Double apply(Teacher teacher) {
                return teacher.getScore();
            }
        }));
        System.out.println(map);


    }
}
